package edu.faytechcc.csc251.elementalTD;

import static com.sun.java.accessibility.util.AWTEventMonitor.addKeyListener;
import java.awt.*;
;

/**
 * The mob class is used for mob creation and physics of mob
 *
 * @author William Robles
 */
public class Mob extends Rectangle
{

    private static int count = 0;

    private int id;

    private int mobX, mobY;
    private int mobSize = 24;

    private int startHealth;
    public int health;
    private static boolean showHealth = false;

    private final int healthSpacing = 3, healthHeight = 5;

    private int mobWalk = 0;
    private int upward = 0, downward = 1, right = 2, left = 3;
    private int direction = downward;

    private int mobID = -1;

    private boolean inGame = false;

    public Mob(int health)
    {        
        this.id = count++;

        this.health = health;
        this.startHealth = health;
    }

    /**
     * spawnMob methods creates a mob based on "mobID", only if it is a
     * groundDirt tile on the first row
     *
     * @param mobID
     */
    public void spawnMob(int mobID)
    {
        if (!isDead())
        {
            for (int x = 0; x < GameDisplay.map.block[0].length; x++)
            {
                if (GameDisplay.map.block[0][x].groundID == GameDisplay.groundDirt)
                {
                    setBounds(GameDisplay.map.block[0][x].x, GameDisplay.map.block[0][x].y, mobSize, mobSize);
                    mobX = x;
                    mobY = 0;
                }
            }

            this.mobID = mobID;
            inGame = true;
        }

    }

    /**
     * deleteMob method removes mob from game and resets variables
     */
    public void deleteMob()
    {
        inGame = false;
        mobWalk = 0;
        direction = downward;
    }

    /**
     * damagePlayer method removes players life based on parameter
     *
     * @param damage
     */
    public void damagePlayer(int damage)
    {
        GameDisplay.playerHealth -= damage;
    }

    /**
     * movement method moves mob based on if the value of the groundDirt tile is
     * next and hasn't moved the way it came
     *
     * also the method contains a check to see if mob has reached the end and
     * removes life and the mob
     *
     * values for mob's speed can be adjusted using walkTimer and walkSpeed
     */
    private int walkTimer = 0, walkSpeed = 10;

    public void movement()
    {
        if (walkTimer >= walkSpeed)
        {
            if (direction == right)
            {
                x++;
            }
            else if (direction == upward)
            {
                y--;
            }
            else if (direction == downward)
            {
                y++;
            }
            else if (direction == left)
            {
                x--;
            }

            mobWalk++;

            if (mobWalk == GameDisplay.map.blockSize)
            {
                if (direction == right)
                {
                    mobX++;
                }
                else if (direction == upward)
                {
                    mobY--;
                }
                else if (direction == downward)
                {
                    mobY++;
                }
                else if (direction == left)
                {
                    mobX--;
                }

                if (GameDisplay.map.block[mobY][mobX].groundID == GameDisplay.exitDirt)
                {

                    deleteMob();
                    damagePlayer(GameDisplay.getMobGold());
                    direction = downward;

                }
                else if (GameDisplay.map.block[mobY + 1][mobX].groundID == GameDisplay.groundDirt && direction != upward)
                {
                    direction = downward;
                }
                else if ((GameDisplay.map.block[mobY - 1][mobX].groundID == GameDisplay.groundDirt
                        || GameDisplay.map.block[mobY - 1][mobX].groundID == GameDisplay.exitDirt) && direction != downward)
                {
                    direction = upward;
                }
                else if (GameDisplay.map.block[mobY][mobX - 1].groundID == GameDisplay.groundDirt && direction != right)
                {
                    direction = left;
                }
                else if (GameDisplay.map.block[mobY][mobX + 1].groundID
                        == GameDisplay.groundDirt && direction != left)
                {
                    direction = right;
                }

                mobWalk = 0;
            }

            walkTimer = 0;
        }
        else
        {
            walkTimer++;
        }
    }

    /**
     * damageMob method removes mob health based on parameter and then checks if
     * mob is dead
     *
     * @param damage
     */
    public void damageMob(int damage)
    {
        health -= damage;
    }

    /**
     * isDead method will check if the mob is dead
     *
     * @return check
     */
    public boolean isDead()
    {
        return (this.health <= 0);
    }

    /*
     * Does the actual drawing of the mob and the mobs healthbar
     */
    public void draw(Graphics g)
    {
        int greenBar = (int) ((double) health / startHealth * mobSize);

        g.drawImage(GameDisplay.tile_mobs[mobID], x, y, width, height, null);
        
        if (showHealth)
        {
            g.setColor(Color.RED);
            g.fillRect(x, y - (healthSpacing + healthHeight), mobSize, healthHeight);

            g.setColor(Color.GREEN);
            g.fillRect(x, y - (healthSpacing + healthHeight), greenBar, healthHeight);

            g.setColor(Color.BLACK);
            g.drawRect(x, y - (healthSpacing + healthHeight), mobSize - 1, healthHeight - 1);
        }
    }

    public boolean isInGame()
    {
        return inGame;
    }

    @Override
    public boolean equals(Object o)
    {
        if (o == null || !(o instanceof Mob))
        {
            return false;
        }
        Mob other = (Mob) o;
        return this.id == other.id;
    }

    /**
     * 
     * @param key 
     */
    public static void setShowHealth(char key)
    {
        if(showHealth == false && key == ' ' )
        {
            showHealth = true;
        }
        else if (showHealth == true && key == ' ')
        {
            showHealth = false;
        }
    }
}
